class Customer::OrdersController < ApplicationController
  def new
    @order = @current_account.user.orders.last
    @order.address = @current_account.user.address
    @restaurants = []
    Restaurant.all.each do |r|
      @restaurants.push r.name
    end
    @vicina = Restaurant.nearest_restaurant @current_account.user.latitude,
                                            @current_account.user.longitude
  end

  def create
    @order = @current_account.user.orders.last

  end

  def addproduct
    # come parametro prende un prodotto e lo aggiunge all'ordine temporaneo
    # params[:product]
    if @current_account.user.orders.empty? or not @current_account.user.orders.last.incomplete?
      @order = Order.new
      @order.customer = @current_account.user
      @current_account.user.orders.push @order
    else
      @order = @current_account.user.orders.last
    end

    @order.content += "#{Product.find(params[:id]).name}, "
    if @order.save
      flash[:notice] = "Il prodotto è stato aggiunto!"
    else
      flash[:error] = "Non è stato possibile aggiungere il prodotto"
    end
    redirect_to products_path
  end
  
  def index
    @orders = @current_account.user.orders
  end

  def all
    @orders = @current_account.user.orders
    render :index
  end

  def edit
  end

  def update
  end

end
